package net.sk.china.front.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sk.china.common.constants.PayConstants;
import net.sk.china.common.exception.TalkException;
import net.sk.china.common.model.order.StoreOrder;
import net.sk.china.front.constants.Constants;
import net.sk.china.front.service.order.StoreOrderService;
import net.sk.china.front.service.system.SystemConfigService;
import net.sk.china.front.service.user.UserService;
import org.springframework.stereotype.Service;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/13 09:53
 * +----------------------------------------------------------------------
 */

@Service
public class OrderUtils {

    private final SystemConfigService systemConfigService;

    private final StoreOrderService storeOrderService;

    private final UserService userService;

    public OrderUtils(SystemConfigService systemConfigService, StoreOrderService storeOrderService, UserService userService) {
        this.systemConfigService = systemConfigService;
        this.storeOrderService = storeOrderService;
        this.userService = userService;
    }

    /**
     * 检查支付类型
     * @param payType 支付类型标识
     * @return {@code boolean}
     */
    public boolean checkPayType(String payType){
        boolean result = false;
        payType = payType.toLowerCase();
        switch (payType){
            case PayConstants.PAY_TYPE_WE_CHAT:
                result = systemConfigService.getValueByKey(Constants.CONFIG_PAY_WEIXIN_OPEN).equals("1");
                break;
            case PayConstants.PAY_TYPE_YUE:
                result = (systemConfigService.getValueByKey(Constants.CONFIG_YUE_PAY_STATUS).equals("1"));
                break;
            case PayConstants.PAY_TYPE_ALI_PAY:
                result = (systemConfigService.getValueByKey(Constants.CONFIG_ALI_PAY_STATUS).equals("1"));
                break;
        }
        return result;
    }

    /**
     * 检测支付渠道
     * @param payChannel 支付渠道
     * @return {@code boolean}
     */
    public static boolean checkPayChannel(String payChannel) {
        if (!payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_H5) &&
                !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_PROGRAM) &&
                !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_PUBLIC) &&
                !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_APP_IOS) &&
                !payChannel.equals(PayConstants.PAY_CHANNEL_WE_CHAT_APP_ANDROID)) {
            return false;
        }
        return true;
    }

    /**
     * h5 订单查询 where status 封装
     * @param query 查询条件
     * @param status 状态
     */
    public void statusApiByWhere(LambdaQueryWrapper<StoreOrder> query, Integer status){
        switch (status){
            case Constants.ORDER_STATUS_H5_UNPAID: // 未支付
                query.eq(StoreOrder::getPaid, false);
                query.eq(StoreOrder::getStatus, 0);
                query.eq(StoreOrder::getRefundStatus, 0);
                query.eq(StoreOrder::getType, 0);
                break;
            case Constants.ORDER_STATUS_H5_NOT_SHIPPED: // 待发货
                query.eq(StoreOrder::getPaid, true);
                query.eq(StoreOrder::getStatus, 0);
                query.eq(StoreOrder::getRefundStatus, 0);
//                query.eq(StoreOrder::getShippingType, 1);
                break;
            case Constants.ORDER_STATUS_H5_SPIKE: // 待收货
                query.eq(StoreOrder::getPaid, true);
                query.eq(StoreOrder::getStatus, 1);
                query.eq(StoreOrder::getRefundStatus, 0);
                break;
            case Constants.ORDER_STATUS_H5_JUDGE: //  已支付 已收货 待评价
                query.eq(StoreOrder::getPaid, true);
                query.eq(StoreOrder::getStatus, 2);
                query.eq(StoreOrder::getRefundStatus, 0);
                break;
            case Constants.ORDER_STATUS_H5_COMPLETE: // 已完成
                query.eq(StoreOrder::getPaid, true);
                query.eq(StoreOrder::getStatus, 3);
                query.eq(StoreOrder::getRefundStatus, 0);
                break;
            case Constants.ORDER_STATUS_H5_REFUNDING: // 退款中
                query.eq(StoreOrder::getPaid, true);
                query.in(StoreOrder::getRefundStatus, 1, 3);
                break;
            case Constants.ORDER_STATUS_H5_REFUNDED: // 已退款
                query.eq(StoreOrder::getPaid, true);
                query.eq(StoreOrder::getRefundStatus, 2);
                break;
            case Constants.ORDER_STATUS_H5_REFUND: // 包含已退款和退款中
                query.eq(StoreOrder::getPaid, true);
                query.in(StoreOrder::getRefundStatus, 1,2,3);
                break;
        }
        query.eq(StoreOrder::getIsDel, false);
        query.eq(StoreOrder::getIsSystemDel, false);
    }

    /**
     * 翻译支付方式给前端
     * @param payType 支付方式
     * @return {@code String}
     */
    public String getOrderPayTypeStr(String payType){
        String payTypeStr = null;
        switch (payType){
            case Constants.PAY_TYPE_WE_CHAT:
                payTypeStr = "微信支付";
                break;
            case Constants.PAY_TYPE_YUE:
                payTypeStr = "余额支付";
                break;
            case Constants.PAY_TYPE_OFFLINE:
                payTypeStr = "线下支付";
                break;
            case Constants.PAY_TYPE_ALI_PAY:
                payTypeStr = "支付宝支付";
                break;
            default:
                payTypeStr = "其他支付方式";
                break;
        }
        return payTypeStr;
    }

    /**
     * 根据订单ID订单信息
     * @param id 订单ID
     * @return 计算后的价格集合
     */
    public StoreOrder getInfoById(Integer id) {
        StoreOrder storeOrder = storeOrderService.getById(id);
        Integer userId = userService.getUserId();

        if(null == storeOrder || !userId.equals(storeOrder.getUid())){
            //订单号错误
            throw new TalkException("没有找到相关订单信息!");
        }

        return storeOrder;
    }
}
